package com.njym.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.njym.annotation.PermInfo;
import com.njym.entity.SysPaymentMode;
import com.njym.service.SysPaymentModeService;
import com.njym.vo.Json;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhy
 * @create 2018-08-21 17:41
 **/
@PermInfo(value = "系统渠道配置")
@RestController
@RequiresPermissions("a:sys:payment")
@RequestMapping("/sys_payment")
public class PaymentModeController {
	private static final Logger log = LoggerFactory.getLogger(PaymentModeController.class);

	@Autowired
	private SysPaymentModeService sysPaymentmodeService;

	@PermInfo("查找渠道信息")
	@RequiresPermissions("a:sys:payment:query")
	@PostMapping("query")
	public Json queryOrganization(@RequestBody String body) {
		String oper = "query payment info";
		log.info("{}, body: {}", oper, body);
		JSONObject json = JSON.parseObject(body);
		//医院id
		String orgId = json.getString("orgId");
		//查询关键词
		String searchWords = json.getString("searchWords");

		List<SysPaymentMode> list =  sysPaymentmodeService.selectConfigsByOrgid(orgId,searchWords);

		return Json.succ(oper).data("list", list);
	}

	@PermInfo("修改渠道信息")
	@RequiresPermissions("a:sys:payment:update")
	@PostMapping("update")
	public Json updateModeValue(@RequestBody String body){
		String oper = "update payment info";
		log.info("{}, body: {}",oper,body);
		SysPaymentMode paymentMode = JSON.parseObject(body, SysPaymentMode.class);
		paymentMode.setDeleted(0);
		Boolean success = sysPaymentmodeService.updateById(paymentMode);
		return Json.result(oper, success);
	}

	@PermInfo("添加渠道信息")
	@RequiresPermissions("a:sys:payment:add")
	@PostMapping("add")
	public Json addModeValue(@RequestBody String body){
		String oper = "add payment info";
		log.info("{}, body: {}",oper,body);
		SysPaymentMode paymentMode = JSON.parseObject(body, SysPaymentMode.class);
		SysPaymentMode paymentModeDB = sysPaymentmodeService.selectOne(new EntityWrapper<SysPaymentMode>().eq("mode_name", paymentMode.getModeName()).eq("org_id",paymentMode.getOrgId()).eq("deleted",0));
		if (paymentModeDB != null) {
			return Json.fail(oper, "该渠道已经建立");
		}
		paymentMode.setDeleted(0);
		boolean success = sysPaymentmodeService.insert(paymentMode);
		return Json.result(oper, success);


	}

	@PermInfo("逻辑删除某一支付手段")
	@RequiresPermissions("a:sys:payment:delete")
	@DeleteMapping
    public Json deleteModeById(@RequestBody String body) {
		String oper = "delete payment mode";
		log.info("{}, body: {}",oper,body);

		JSONObject jsonObj = JSON.parseObject(body);
		String id = jsonObj.getString("id");
		boolean success =  sysPaymentmodeService.deleteById(id);
		return Json.result(oper, success);
    }
}
